<template>
    <div>
       <FormPanel
        :list="topFilterList"
        :cols="3"
        formType="show"
        :isSubmitBtn="false"
        :collapse="false"
      ></FormPanel>
      <hr>
      <FormPanel
        :list="topFilterList2"
        :cols="3"
        formType="show"
        :isSubmitBtn="false"
        :collapse="false"
      ></FormPanel>
        <!-- 内容 -->
        <tinymce v-model="content" :disabled="true" :isUploadImage="false" :height="300" actionUrl="/api/basedata/upload" />
        <div style="margin-top:5px;">
            <span>上传附件：</span>
            <span style="cursor: pointer;" @click="downloadFile">{{fileName}}</span>
        </div>
        <div class="dialogBottmView" style="text-align: center;margin-top:20px;">
        <el-button @click="closeDialog()">返 回</el-button>
        </div>
    </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {formatDates,formatDat} from '@/utils/validate';
import {getComOrgTreeList,getComOrgTreeLists} from '@/api/common/org/employeeInfo';
import { getJsonTree } from '@/utils/index';
import axios from 'axios'

export default {
    name: 'info',
    mixins: [dictionary],
    props:["formData"],
    data() {
        return {
        topFilterList: this.createTopFilters(),
        topFilterList2: this.createTopFilters2(),
        content: this.formData.notificationContent,
        fileUrl:"",
        fileName:"",
        };
    },
   
    methods:{
        createTopFilters() {
            return [   
                {
                type: 'SELECT',
                label: '公告类别',
                fieldName: 'notificationType',
                placeholder: '公告类别',
                style: { width: '240px' },
                itemList: this.createDictList('2561'),
                }, 
                {
                type: 'DATE',
                label: '发布时间',
                fieldName: 'publishDate',
                placeholder: '发布时间',
                initialValue: '',
                dateType:'datetime',
                style: { width: '240px' },
                },
                {
                type: 'DATE',
                label: '有效期至',
                fieldName: 'endTime',
                placeholder: '请输入...',
                initialValue: '',
                style: { width: '240px' },
                },
                {
                type: 'SELECT',
                label: '公告来源',
                fieldName: 'dataType',
                placeholder: '公告来源',
                style: { width: '240px' },
                itemList: this.createDictList('1046'),
                },
                {
                type: 'INPUT',
                label: '发布部门',
                fieldName: 'publishOrgId',
                placeholder: '发布部门',
                style: { width: '240px' },
                },
                {
                type: 'INPUT',
                label: '发布人',
                fieldName: 'publishPerson',
                placeholder: '发布人',
                style: { width: '240px' },
                },
                 {
                type: 'SELECT',
                label: '紧急程度',
                fieldName: 'urgency',
                placeholder: '请输入...',
                initialValue: '',
                style: { width: '240px' },
                itemList: this.createDictList('2562'),
                },
            ];
        },
        createTopFilters2() {
            return [   
                {
                type: 'INPUT',
                label: '公告标题',
                fieldName: 'notificationTitle',
                placeholder: '通知类型',
                style: { width: '240px' },
                }
            ];
        },
        init(){
            console.log(this.formData,"formData")
            this.topFilterList.forEach(ele=>{
                ele.initialValue = this.formData[ele.fieldName]
            })

            this.fileUrl=this.formData.fileUrl
            this.fileName=this.formData.fileName
            this.topFilterList[1].initialValue=formatDates(this.formData.publishDate)
            this.topFilterList[2].initialValue=formatDat(this.formData.endTime)
            this.topFilterList2[0].initialValue =this.formData.notificationTitle 

        },
        // 部门树
        async inputTree() {
            const res = await getComOrgTreeList();
            const res2 = await getComOrgTreeLists()
            
            var that = _.uniq(res.data.concat(res2.data))
            console.log(that,"that",this.formData)
            // that.forEach(el => (el.text = el.text));
            // that.forEach(el => (el.value = el.orgId));
            // that.forEach(el => (el.parentId = el.parent));
            that.forEach(x=>{
                if(x.orgId == this.formData.publishOrgId){
                    console.log(x,"x")
                    this.topFilterList.find(item=>item.fieldName == "publishOrgId").initialValue =x.text
                }
            })
            // console.log(this.topFilterList.find(item=>item.fieldName == "publishOrgId").itemList,"orgName123")
            // if(this.value.type != 'add'){
            // this.formList[3].initialValue=`${this.formList.find(item=>item.fieldName == "publishOrgId").initialValue}`
            // console.log(this.formList[3].initialValue,"this.formList")
            // }
            // console.log(this.formList[3].initialValue,this.formList[3].placeholder,"placeholder")
        },
        // 获取服务端文件 to blob
        async downLoadByUrl(url, params = {}) {
        return await axios({ url, params, responseType: 'blob' });
        },
        // 执行下载动作
        async downloadFile(url) {
            if(this.fileName.split(".")[1] == 'jpg' || this.fileName.split(".")[1] == 'png'){
                return window.open(this.fileUrl)
            }    
            const { headers, data } = await this.downLoadByUrl(this.fileUrl);
            const contentDisposition = headers['content-disposition'];
            // 获取文件名
            const fileName = contentDisposition ? contentDisposition.split(';')[1].split('filename=')[1] : !this.fileName ? url.slice(url.lastIndexOf('/') + 1) : this.fileName;
            // ie10+
            if (navigator.msSaveBlob) {
                navigator.msSaveBlob(data, decodeURI(fileName));
            } else {
                const downloadUrl = window.URL.createObjectURL(data);
                let a = document.createElement('a');
                a.href = downloadUrl;
                a.download = decodeURI(fileName);
                a.click();
                a = null;
            }
        },
        // 关闭当前页面操作
        closeDialog(){
            this.$emit('close');
        },

    },
     mounted() {
        this.init()
        this.inputTree(); 
    },
}
</script>

<style>

</style>
